package com.letoken.platform.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.app.service.AppMiningDetailService;
import com.letoken.platform.pub.annotation.auth.NotAuthorization;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.dao.AdminMiningInfoMapper;
import com.letoken.platform.repo.admin.po.AdminMiningInfo;
import com.letoken.platform.repo.app.dao.AppMiningDetailMapper;
import com.letoken.platform.repo.app.dao.AppMiningInfoMapper;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.po.AppFundDetail;
import com.letoken.platform.repo.app.po.AppMiningDetail;
import com.letoken.platform.repo.app.po.AppMiningInfo;
import com.letoken.platform.repo.app.req.user.MiningDetailReq;
import com.letoken.platform.repo.app.res.config.QueryHistoryRes;
import com.letoken.platform.repo.app.res.user.AppLpRes;
import com.letoken.platform.repo.utlis.PageDataUtil;
import com.letoken.platform.repo.utlis.WebUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/appMiningController")
public class AppMiningController {

    @Autowired
    private AppMiningDetailService appMiningDetailService;

    @Resource
    private AppMiningDetailMapper appMiningDetailMapper;

    @Resource
    private AdminMiningInfoMapper adminMiningInfoMapper;
    @Resource
    private AppMiningInfoMapper appMiningInfoMapper;



    @GetMapping("/pledge")
    public ApiResponse pledge(@RequestParam("amount") BigDecimal amount) {
        if(amount.compareTo(BigDecimal.ONE)<0){
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_MALL_SMS_052);
        }
        appMiningDetailService.pledge(amount);
        return ApiResponse.create();
    }

    @GetMapping("/home")
    public ApiResponse<Map<String,Object>> home() {
        Map<String,Object> map = new HashMap<>();
        Integer userId = WebUtils.getUserId();
        AppMiningInfo appMiningInfo = appMiningInfoMapper.selectOne(new QueryWrapper<AppMiningInfo>().eq("user_id",userId));
        if(ObjectUtils.isEmpty(appMiningInfo)){
            map.put("pledge",0);
            map.put("computing",0);
            map.put("sum",0);
            map.put("yesterday",0);
        }else{
            AdminMiningInfo adminMiningInfo = adminMiningInfoMapper.selectById(1);
            map.put("pledge",appMiningInfo.getAmount());
            map.put("computing",adminMiningInfo.getDailyNumber().divide(adminMiningInfo.getSumNumber(),10, RoundingMode.HALF_UP).multiply(appMiningInfo.getAmount()));
            map.put("sum",appMiningInfo.getAccumulateIncome());
            map.put("yesterday",appMiningDetailMapper.getYesterdayIncome(userId));
        }
        return ApiResponse.create(map);
    }

    @PostMapping ("/getMiningDetail")
    public ApiResponse<PageData<AppMiningDetail>> getMiningDetail(@Validated @RequestBody MiningDetailReq req) {
        Integer userId = WebUtils.getUserId();
        Page<AppMiningDetail> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        return ApiResponse.create(PageDataUtil.getPageData(appMiningDetailMapper.getMiningDetail(userId,page,req.getType())));
    }

    @GetMapping  ("/test")
    @NotAuthorization
    public ApiResponse test() {
        appMiningDetailService.mining();
        return ApiResponse.create();
    }
}
